#!/usr/bin/make -f

package = apex-nslu2
docdir = debian/tmp/usr/share/doc/$(package)

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
CONFIG	      := debian-nslu2-$(DEB_HOST_ARCH)_config

CC = gcc
CFLAGS = -g -Wall
INSTALL_PROGRAM = install


ifeq (arm,$(DEB_HOST_ARCH))
 ENDIAN:=l
endif
ifeq (armel,$(DEB_HOST_ARCH))
 ENDIAN:=l
endif
ifeq (armeb,$(DEB_HOST_ARCH))
 ENDIAN:=b
endif

# We must not change the build options.
#ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#  CFLAGS += -O2
#endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  INSTALL_PROGRAM += -s
endif

build:
	$(checkdir)
	$(MAKE) $(CONFIG)
	$(MAKE)
	$(MAKE) -C usr CROSS_COMPILE=
	touch build

clean:
	$(checkdir)
	rm -f build
	-$(MAKE) -i distclean
	rm -rf *~ debian/tmp debian/*~ debian/files* debian/substvars

binary-indep:	checkroot build
	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch:	checkroot build
	$(checkdir)
	rm -rf debian/$(package)
	dh_testroot -a
	dh_installdirs
	install -d debian/$(package)/boot
	install -d debian/$(package)/usr/sbin
	install -m 0644 src/arch-arm/rom/apex.bin\
		  debian/$(package)/boot/apex.flash
	install -m 0755 usr/apex-env debian/$(package)/usr/sbin
	install -m 0755 usr/flash-apex debian/$(package)/usr/sbin
	install -m 0755 usr/apex-image debian/$(package)/usr/sbin
	dh_strip
	dh_installchangelogs ChangeLog
	dh_installdocs -a
	dh_installman usr/apex-env.8
	dh_installman usr/flash-apex.8
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

define checkdir
	test -f src/apex/init.c -a -f debian/rules
endef

binary: binary-indep binary-arch

checkroot:
	$(checkdir)
	test $$(id -u) = 0

.PHONY: binary binary-arch binary-indep clean checkroot
